package org.qydjk.h5.controller.product;

import org.qydjk.common.constant.RedisConstant;
import org.qydjk.common.pojo.Area;
import org.qydjk.common.pojo.product.Product;
import org.qydjk.common.pojo.query.QueryUserComment;
import org.qydjk.common.util.CookieUtil;
import org.qydjk.common.util.RedisTemplateUtil;
import org.qydjk.common.vo.PaginationInfo;
import org.qydjk.common.vo.redisvo.RedisSellerProductLabel;
import org.qydjk.h5.controller.base.BaseController;
import org.qydjk.service.service.area.IAreaService;
import org.qydjk.service.service.area.impl.AreaService;
import org.qydjk.service.service.customerCenter.ICustomerCollectionService;
import org.qydjk.service.service.customerCenter.ICustomerCommentService;
import org.qydjk.service.service.product.IProductService;
import org.qydjk.service.vo.ProductList;
import org.qydjk.service.vo.product.ProductListParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created by Deng_Junwei on 2018/1/8.
 */
@Controller
@RequestMapping("/product/")
public class ProductController extends BaseController {

    @Autowired
    private IAreaService areaService;
    @Autowired
    private IProductService productService;
    @Autowired
    private RedisTemplateUtil redisTemplateUtil;
    @Autowired
    private ICustomerCommentService customerCommentService;//评论
    @Autowired
    private ICustomerCollectionService customerCollectionService;//收藏

    @RequestMapping("/gotoProductList.do")
    public String gotoProductList(Model model,String productTag){
        try {
        //Redis后台初始化
            List<RedisSellerProductLabel> sLabelList = (List<RedisSellerProductLabel>) redisTemplateUtil.getWithByte(RedisConstant.SELLER_LABEL_KEY);
            List<RedisSellerProductLabel> pLabelList = (List<RedisSellerProductLabel>) redisTemplateUtil.getWithByte(RedisConstant.PRODUCT_LABEL_KEY);

            Integer cityId = 2;
            List<Area> areaList = areaService.findAreaByParentId(cityId);

            model.addAttribute("sLabelList",sLabelList);
            model.addAttribute("pLabelList",pLabelList);
            model.addAttribute("areaList",areaList);
            model.addAttribute("productTag",productTag);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return "index/productList";
    }

    @RequestMapping("/productList.do")
    @ResponseBody
    public Object productList(Integer type, Integer page, Integer areaId, String sCode, String pCode, String ordWay, String eCode) {
        try {
            ProductListParam param = new ProductListParam();
            Integer pageSize = 5;//每页显示条数

            //设置经纬度
            String[] latLon = getLatLon();
            param.setLon(150.14);
            param.setLat(132.12);

            //根据页面type参数判断根据什么字段排序
            String ordColumn = "";
            if (type == null || type == 0) {
                ordColumn = "ord";
                if (ordWay == null) {
                    ordWay = "DESC";
                }
            } else if (type == 1) {
                ordColumn = "sales_count";
                if (ordWay == null) {
                    ordWay = "DESC";
                }
            } else if (type == 2) {
                ordColumn = "price";
            }

            //排序方式
            if ("0".equals(ordWay)) {
                ordWay = "ASC";
            } else if ("1".equals(ordWay)) {
                ordWay = "DESC";
            }

            //价格筛选
            if (eCode != null) {
                String[] eCodes = eCode.split(",");
                if (!"".equals(eCodes[0])) {
                    param.setLowPrice(new BigDecimal(eCodes[0]));
                }
                if (eCodes.length > 1 && !"".equals(eCodes[1])) {
                    param.setHighPrice(new BigDecimal(eCodes[1]));
                }
            }
            param.setOrdColumn(ordColumn);
            param.setOrdWay(ordWay);
            param.setPage(page);
            param.setPageSize(pageSize);  //每页加载数据条数
            param.setSellerTag(sCode);
            param.setProductTag(pCode);
            param.setAreaId(areaId);
            param.setCityId(getAreaId());
            param.setIsCount(true);

            PaginationInfo paginationInfo = productService.findProductList(param);
            List<ProductList> list = paginationInfo.getList();
            return renderListSuccess(list, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /***
     * 前往产品详情
     * @author Edward_yang
     * @uptime 2018-01-11
     * @param productId
     * @return
     */
    @RequestMapping("/gotoProductDetail.do")
    public String gotoProductDetail(Integer productId){
        try {
            //TODO map 两个对象 一个商家seller 一个产品product
            Map<String,Object> map=productService.findProductInfo(productId);
            List<Map<String ,Object>> producImgsList=productService.findProducImgs(productId);
            List<Map<String ,Object>> productServeLabelList=productService.findProductServeLabel(productId);
            //TODO 相关产品 3个一组
            List<Map<String ,Object>> productRelatedList=productService.findProductRelatedList(productId);

            request.setAttribute("product",map.get("product"));
            request.setAttribute("seller",map.get("seller"));
            request.setAttribute("producImgsList",producImgsList);
            request.setAttribute("productServeLabelList",productServeLabelList);//服务标签
            request.setAttribute("productRelatedList",productRelatedList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/index/productDetail";
    }

    /***
     * 请求产品评论的分页数据
     * @author Edward_yang
     * @uptime 2018-01-11
     * @param page
     * @param rows
     * @param productId
     * @return
     */
    @RequestMapping("/productCommentList.do")
    @ResponseBody
    public Object  productCommentList(Integer page, Integer rows,Integer productId ){
        if(rows==null){
            rows=5 ;
        }
        List<QueryUserComment> queryUserCommentList= null;
        try {
            //TODO 评分 isDummy 是否虚假评论：0否 1是，null 全部
            queryUserCommentList = customerCommentService.findCustomerCommentList( page,  rows, null,productId,null);
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
        return renderListSuccess(queryUserCommentList, rows);
    }

    /***
     * 点击收藏 /取消收藏
     * @author Edward_yang
     * @uptime 2018-01-11
     */
    @RequestMapping("/onclickProductCollect.do")
    @ResponseBody
    public Object onclickSellerCollect(Integer userId,Integer productId ,Boolean isCollect){
        userId=1;
        try {
            //TODO type 收藏类型：0产品1商家2文章
            //TODO isCollect 取反
            customerCollectionService.addonclickUserCollect(userId,productId,0,isCollect);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }

    }
}
